package com.hongye.app.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;

public class GoodsCategroyModel extends Model<GoodsCategroyModel>{
	private static final long serialVersionUID =  -5317310510318889388L;
	public static final GoodsCategroyModel dao = new GoodsCategroyModel();
	
	/**
	 * 获取用户编辑时的所有兴趣类别
	 * @author lifuqiang
	 * 2016-12-16
	 * @param categroyid
	 * @return
	 */
	public List<GoodsCategroyModel> getCategroyByCategroyid(String categroyid){
		String sql2 = "";
		if( !categroyid.equals("") ){
			sql2 = "select * from goods_categroy where pid <100 and id not in("+ categroyid+")";
		}else{
			sql2 = "select * from goods_categroy where pid <100 ";
		}
		List<GoodsCategroyModel> gcm_list = dao.find(sql2);
		return gcm_list;
	}
	
	/**
	 * 搜索-获取推荐类别
	 * @author lifuqiang
	 * 2017-1-4
	 * @param cateids
	 * @return
	 */
	public List<GoodsCategroyModel> getHotGoodsCateBySearch(String cateids){
		String sql = "SELECT * from goods_categroy where id in("+cateids+")";
		List<GoodsCategroyModel> goodscate_list_HotSearch = dao.find(sql);
		return goodscate_list_HotSearch;
	}

	/**
	 * 根据id得到商品类别名称
	 * @author liushaomin
	 * 2017年2月28日
	 * @param id
	 * @return
	 */
	public static GoodsCategroyModel getCategroyName(String id) {
		return dao.findById(id);
	}

	/**
	 * 根据一级类别pid 获取所有二级的id(逗号分割)
	 * @author liushaomin
	 * 2017年2月28日
	 * @param pid
	 * @return
	 */
	public static String getTwoLevelid(int pid) {
		String sql = "select * from goods_categroy where showg=0 and pid > "+ pid +" * 100 and pid < ("+ pid +" + 1) * 100 order by pid";
		List<GoodsCategroyModel> categroys = dao.find(sql);
		StringBuffer ids = new StringBuffer();
		for (GoodsCategroyModel item : categroys) {
			ids.append(item.getInt("id")).append(",");
		}
		return ids.substring(0,ids.length()-1);
	}
	
	/**
	 * 通过商品类别ID获取相对应的PID
	 * @author lifuqiang
	 * 2017-4-27
	 * @param CategoryID
	 * @return
	 */
	public static String getCategoryPID_By_ID(String CategoryID){
		String[] cateid_bytes = CategoryID.split(",");
		StringBuffer catepid = new StringBuffer();
		for (int i = 0; i < cateid_bytes.length - 1; i++) {
			GoodsCategroyModel cate = dao.findById(cateid_bytes[i]);
			catepid = catepid.append(cate.get("pid").toString() + ",");
		}
		catepid = catepid.append( dao.findById(cateid_bytes[cateid_bytes.length - 1]).get("pid").toString());
		return catepid.toString();
	}

	/**
	 * 根据经营范围id得到店铺经营范围的类别名称
	 * 后台用
	 * @author liushaomin
	 * 2017年5月5日
	 * @param shop_saletype 店铺销售意向（商品类型的id）
	 * @return
	 */
	public static String getShop_saletypeName(String shop_saletype) {
		String [] ids = shop_saletype.split(",");
		StringBuffer strb = new StringBuffer();
		for (String id : ids) {
			strb.append(getCategroyName(id).get("c_name")).append(" ");
		}
		return strb.toString();
	}
}
